package cn.tedu.crm.customer.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.tedu.crm.base.pojo.PageObject;
import cn.tedu.crm.base.response.JsonResult;
import cn.tedu.crm.customer.pojo.dto.CustomerAddParam;
import cn.tedu.crm.customer.pojo.dto.CustomerFollowUpParam;
import cn.tedu.crm.customer.pojo.dto.CustomerInsertParam;
import cn.tedu.crm.customer.pojo.dto.CustomerQuery;
import cn.tedu.crm.customer.pojo.entity.District;
import cn.tedu.crm.customer.pojo.vo.CustomerListVO;
import cn.tedu.crm.customer.pojo.vo.DistrictVO;
import cn.tedu.crm.customer.pojo.vo.EditCustomerVO;
import cn.tedu.crm.customer.service.CustomerService;

import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Api(tags = "客户模块")
@RestController
@RequestMapping("/api/crm/customer")
@Slf4j
public class CustomerController {

    @Autowired
    private CustomerService customerService;

    @SaCheckPermission("crm:customer:add")
    @PostMapping("/add")
    public JsonResult addCustomer(@Validated @RequestBody CustomerAddParam param) {
        //log.debug("处理【新增客户】，参数：{}", param);
        customerService.addCustomer(param);
        return JsonResult.success(param);
    }

    @SaCheckPermission("crm:customer:select")
    @GetMapping("/select")
    public JsonResult selectCustomerList(CustomerQuery query) {
        PageObject<CustomerListVO> pageObject = PageObject.page(
                query.getPageNum(), query.getPageSize(), () -> customerService.selectCustomerList(query));
        //log.debug("pageObject{}",pageObject);
        return JsonResult.success(pageObject);
    }

    @SaCheckPermission("crm:customer:del")
    @PostMapping("/delete/{id}")
    public JsonResult deleteCustomer(@PathVariable Long id){
        customerService.deleteCustomer(id);
        return JsonResult.ok();
    }


    @SaCheckPermission("crm:customer:select")
    @GetMapping("/selectEditCustomer")
    public JsonResult SelectEditCustomer( CustomerQuery query){
        List<EditCustomerVO>  list  = customerService.SelectEditCustomer(query);
        return  JsonResult.ok(list);
    }

    @GetMapping("deleteImg")
    public JsonResult deleteImg( String imgUrl){
        customerService.deleteImg(imgUrl);
        return JsonResult.ok();
    }


    @PostMapping("updateFollowUp")
    public JsonResult updateFollowUp(@RequestBody CustomerFollowUpParam param){
        customerService.updateFollowUp(param);
        return JsonResult.ok();
    }

    /**
     * 查询客户区域
     * @param district
     * @return
     */
    @GetMapping("selectDistrict")
    public JsonResult selectDistrict(District district){
         List<DistrictVO> list = customerService.selectDistrict(district);
        return JsonResult.ok(list);
    }


    //移动端新增客户
    @PostMapping("insertCustomer")
    public JsonResult insertCustomer(@RequestBody CustomerInsertParam customerInsertParam){
        customerService.insertCustomer(customerInsertParam);
        return JsonResult.ok();
    }


    //移动端新增客户跟进
    @PostMapping("insertCustomerFollowUp")
    public JsonResult insertCustomerFollowUp(@RequestBody CustomerInsertParam customerInsertParam){
        customerService.insertCustomerFollowUp(customerInsertParam);
        return JsonResult.ok();
    }

}
